home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15359 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: user2.mnsinc.com!huang
  2. From: huang@mnsinc.com (Szu-Wen Huang)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: [Q] unpack 10 bit data
  5. Date: 18 Apr 1996 17:34:46 GMT
  6. Organization: Monumental Network Systems
  7. Message-ID: <4l5ufn$79p@news1.mnsinc.com>
  8. References: <Pine.SGI.3.91.960417231151.16763A-100000@anchor.gsfc.nasa.gov> <829831450snz@genesis.demon.co.uk>
  9. NNTP-Posting-Host: user.mnsinc.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Lawrence Kirby (fred@genesis.demon.co.uk) wrote:
  13. : In article <Pine.SGI.3.91.960417231151.16763A-100000@anchor.gsfc.nasa.gov>
  14. :            jyli@anchor.gsfc.nasa.gov "Jason Y. Li" writes:
  15.  
  16. : >I am looking for an efficient and elegant way of unpacking 10 bit data. 
  17. : >In many scientific applications, raw data are packed into 10 bit data 
  18. : >stream.  For example, an input buffer of 40 bits (5 characters) contains 
  19. : >four 10-bit integers.
  20. : > I know one way of unpacking the data which involves 
  21. : >bit masking and then shifting left-and-right. It looks pretty messy to me. I 
  22. : >am sure smart folks out there have better ways or ideas. I'd appreciate 
  23. : >if you would let me know.
  24.  
  25. : The solution will most likely involve shifting and masking. However there
  26. : may perhaps be a cleaner solution than the one you saw.
  27.  
  28. This is strictly for the purpose of giving you an alternative solution,
  29. and may or may not be better than the mask/shift approach.  Theoretically,
  30. if you had enough memory, you can build a huge look-up table indexed by
  31. the 40-bit key.  Each entry in the table will the contain the ordered
  32. quadruples of 10-bit integers.  I doubt even NASA would devote this kind
  33. of memory to solve this triviality :)
  34.  
  35. Therefore, we compromise.  You can take the first 2 bytes and use it to
  36. index table 1, which contains the first 10-bit integer.  Then, take the
  37. second and the third bytes to index table 2, another table that gives
  38. you your second 10-bit integer.  Then bytes 3 and 4, then 4 and 5.  You
  39. will need 4 tables of 64K elements each, 10 bits per element (make it
  40. 16 to be easy).  That takes about 512K of memory, and gives you the
  41. performance of requiring just one memory read per 10-bit integer.
  42. Comparing that with essentially 2 masks, 2 shifts, and an OR, this
  43. memory hog just might be useful - that is, if you really need it ;).
  44.